package com.ykh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.ykh.mapper.FocusedMapper;
import com.ykh.mapper.MessageMapper;
import com.ykh.mapper.UserMapper;
import com.ykh.mapper.VideoMapper;
import com.ykh.pojo.po.Focused;
import com.ykh.pojo.po.Message;
import com.ykh.pojo.po.User;
import com.ykh.pojo.po.Video;
import com.ykh.pojo.vo.Result;
import com.ykh.pojo.vo.VideoMessageVo;
import com.ykh.service.MessageServie;
import com.ykh.util.QiniuUtils;
import com.ykh.util.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 202;
 * 3-06-08
 * Time: 14:00
 */
@Service
public class MessageServiceImpl implements MessageServie {
    @Autowired
    MessageMapper messageMapper;

    @Autowired
    VideoMapper videoMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    FocusedMapper focusedMapper;

    /**
     * 获取自己的消息列表
     * @param
     * @return
     */
    @Override
    public Result getMessageList(User user) {
        //1. 查出该用户作为接收方的 信息
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getAcceptUserId, user.getAccount());
        List<Message> messages = messageMapper.selectList(queryWrapper);

        List<Integer> videoIdList = new ArrayList<>();
        List<String> authorIdList = new ArrayList<>();
        if( messages.size()==0){
            return Result.fail(405,"暂无消息");
        }

        //遍历消息得到视频的id集合
        for(Message message: messages){
            videoIdList.add(message.getVideoId());
        }
        //根据视频id 查出视频信息
        LambdaQueryWrapper<Video> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(Video::getVideoId, videoIdList);

        List<Video> videoList = videoMapper.selectList(queryWrapper1);
        //转化为vo
        List<VideoMessageVo> videoMessageVos = copyList(messages);

        //设置查出作者名称
        LambdaQueryWrapper<User> queryWrapper2 = new LambdaQueryWrapper<>();
        for (Video video: videoList) {
            authorIdList.add(video.getUserAccount());
        }
        queryWrapper2.in(User::getAccount, authorIdList);
        List<User> sysUsers = userMapper.selectList(queryWrapper2);

        for(int i = 0; i<videoMessageVos.size(); i++){
            if(i < sysUsers.size()){
                videoMessageVos.get(i).setLaunchUserName( sysUsers.get(i).getNickName());
                videoMessageVos.get(i).setAvatar( sysUsers.get(i).getAvatar());
            } else {
                videoMessageVos.get(i).setLaunchUserName( sysUsers.get(sysUsers.size()-1).getNickName());
                videoMessageVos.get(i).setAvatar( sysUsers.get(sysUsers.size()-1).getAvatar());
            }
            videoMessageVos.get(i).setIsRead( messages.get(i).getIsRead());
            videoMessageVos.get(i).setCoverUrl(videoList.get(i).getCoverUrl());
            videoMessageVos.get(i).setVideoTitle(videoList.get(i).getVideoTitle());
        }
        return Result.success( videoMessageVos);
    }

    /**
     * 发布视频推送消息给关注他的用户
     * @param
     * @return
     */
    @Override
    public Result addVideoMessage(Integer videoId, User user) {
        List<String> acceptIdList = new ArrayList<>();
        //先查关注表, 获取他的用户id列表
        LambdaQueryWrapper<Focused> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Focused::getFocusedId, user.getAccount());
        List<Focused> focuseds = focusedMapper.selectList(queryWrapper);
        for (Focused focused:focuseds) {
            acceptIdList.add(focused.getLaunchId());
        }

        //构造消息, 往消息表插入
        for(int i=0; i< acceptIdList.size();i++){
            Message message = new Message();
            message.setVideoId( videoId );
            message.setAcceptUserId(acceptIdList.get(i));
            message.setLaunchUserId(user.getAccount());
            message.setIsRead(0);
            message.setCreateDate(LocalDateTime.now());
            message.setContent("我发布了新作品，快来看看把！");
            messageMapper.insert(message);
        }
        return Result.success("消息推送成功");
    }
//
//    @Override
//    public Result addFocusMessage(String acceptUserId, User user) {
//        //先查关注表, 获取他的用户id列表
//        LambdaQueryWrapper<Focused> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Focused::getFocusedId,acceptUserId ).eq(Focused::getLaunchId, user.getAccount());
//        Focused focused = focusedMapper.selectOne(queryWrapper);
//        if(focused == null){
//            return Result.fail(405,"未关注");
//        }
//
//        Message message = new Message();
//        message.setVideoId(null);
//        message.setAcceptUserId(acceptUserId);
//        message.setLaunchUserId(user.getAccount());
//        message.setIsRead(0);
//        message.setType(1);
//        message.setCreateDate(LocalDateTime.now());
//        message.setContent("有新用户关注了你，快来看看把！");
//        messageMapper.insert(message);
//        return Result.success("消息推送成功");
//    }

    private List<VideoMessageVo> copyList(List<Message> messages) {
        List<VideoMessageVo> articleMessageVos = new ArrayList<>();
        for (Message message: messages) {
            articleMessageVos.add( copy(message));
        }
        return articleMessageVos;
    }

    private VideoMessageVo copy(Message message) {
        VideoMessageVo videoMessageVo = new VideoMessageVo();
        BeanUtils.copyProperties(message, videoMessageVo);
        videoMessageVo.setCreateDate(TimeUtil.getDateTimeAsString(message.getCreateDate()));
        return videoMessageVo;
    }
}
